package com.wang.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 准备一个线程池
 * @author lby
 *
 */
public class ExecutorsUtil
{
	/*线程池*/
	private static ExecutorService executorService = Executors.newCachedThreadPool();
	
	/**
	 * 提交一个线程,同时不等待
	 * @param task
	 */
	public static void submit(Callable<Object> task)
	{
		executorService.submit(task);
	}
	/**
	 * 等待提交线程
	 */
	public static void invokeAll(List<Callable<Object>>taskList)
	{
		try
		{
			executorService.invokeAll(taskList);
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 停止
	 */
	public static  void showdowm()
	{
		/*停止，有停止的方法，但是不要停止*/
		executorService.shutdown();
	}
	
	public static void main(String[]args)
	{
		ExecutorsUtil executorsUtil = new ExecutorsUtil();
		
		List<Callable<Object>> taskList = new ArrayList<Callable<Object>>();
		
		taskList.add(() ->{
			for(int i = 0 ; i < 10 ; i++)
			{
				System.out.println(Thread.currentThread()+"==线程=="+i);
			}
			return null ;
		});
		
		executorsUtil.invokeAll(taskList);
		System.out.println(Thread.currentThread()+"==main==");
	}
}
